import java.io.*;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.URLEncoder;

public class PostRequest {
    public static void main(String[] args){
        sendPost();
    }


    public static void sendPost() {
        Socket socket = new Socket();
        String path = "/servlet/UploadHandleServlet";
        String host = "localhost";
        String data = " name=\"file1\"; filename=\"a.txt\"";
        SocketAddress socketAddress = new InetSocketAddress(host,8080);
        try {
            socket.connect(socketAddress);
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(socket.getOutputStream());
            BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
            bufferedWriter.write("POST " + path + " HTTP/1.1\r\n");
            bufferedWriter.write("Host: " + host + "\r\n");
            bufferedWriter.write("Content-Length: " + data.length() + "\r\n");
            bufferedWriter.write("Content-Type:  text/plain\r\n");
            bufferedWriter.write("\r\n");
            bufferedWriter.write(data);
            bufferedWriter.flush();
            bufferedWriter.write("\r\n");
            bufferedWriter.flush();

            BufferedInputStream bufferedInputStream = new BufferedInputStream(socket.getInputStream());
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(bufferedInputStream));
            String line = null;
            while((line = bufferedReader.readLine())!= null)
            {
                System.out.println(line);
            }
            bufferedReader.close();
            bufferedWriter.close();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
